<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class One extends Controller
{
    //用DB增删改查  接口 一套必须写路由Route::post('create','One@create');
    public function create(Request $request){
        $arr=$request->post();
        //文件上传
        $path = $request->file('avatar')->store('avatars');
        $arr['create_time']=time();

        $res=DB::table('article')->insert($arr);
        if ($res){
            return ['code'=>200,'msg'=>'添加成功','data'=>$res];
        }
    }
    //展示  获取多条用get Route::get('show','One@show');
    public function show(){
        $data=DB::table('article')->get();
        return ['code'=>200,'msg'=>'展示成功','data'=>$data];
    }
    //详情 展示 Route::get('info','One@info');
    public function info(){
        $id=\request()->get('id');
        $res=DB::table('article')->where('id',$id)->find($id);
        return ['code'=>200,'msg'=>'展示详情','data'=>$res];
    }
    //修改  Route::post('update','One@update');   http://www.exam11.com/api/update?id=2
    public function update($id){
//        $id=\request()->get('id');
        $arr=\request()->post();
        $res=DB::table('article')->where('id',$id)->update($arr);
        if ($res){
            return ['code'=>200,'msg'=>'修改成功','data'=>$arr];
        }else{
            return ['code'=>201,'msg'=>'修改失败','data'=>null];
        }
    }
    //删除
    public function del(){
        $id=\request()->get('id');
        $res=DB::table('article')->where('id',$id)->delete();
        if ($res){
            return ['code'=>200,'msg'=>'删除成功','data'=>$res];
        }
    }
}
